<?php
include_once("sessionmanager.php");
initSession();

if (!isset($_SESSION['s_loggedin']) || ($_SESSION['s_loggedin'] == false) || !isset($_SESSION['s_serverurl']) || (strpos($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],$_SESSION['s_serverurl']) === false)) {
	$error_page_code = "timeout";
	include "errorpage.php";
	exit();
}
if (!$_SESSION['s_allyhistory']) {
	$error_page_code = "permission";
	include "errorpage.php";
	exit();
}
include "languages.php";
include_once "functions.inc.php";
include_once "config.php";
include "attributes.php";

include "jpgraph/jpgraph.php";
include "jpgraph/jpgraph_line.php";

makeheader(ALLYHISTORY_TITLE);

mysql_connect($dbhost,$dbusername,$dbpassword) or die(PARSER_ERROR3);
mysql_select_db($dbname) or die(PARSER_ERROR2);


if (isset($_GET['details']) && !empty($_GET['details']) && is_numeric($_GET['details'])) {
	// determine if monthly stats are available
	$query = "SELECT count(DISTINCT(substring(date_added, 1, 7))) AS anzahl FROM $allyhistory WHERE ogame_playerid=".$_GET['details']." GROUP BY substring( date_added, 1, 7 )";
	$res = mysql_query($query) or die(mysql_error());
	$line = mysql_fetch_object($res);
	$count = $line->anzahl;

	// show history
	echo '
	<table cellpadding="4" cellspacing="0" border="0" width="90%" align="center">
	<tr class="tblhead"><td>'.ALLYHISTORY_TBLHEAD.'</td></tr>
	<tr class="firstcolor"><td><img src="graph.php?typ=month&amp;playerid='.$_GET['details'].'&amp;text='.ALLYHISTORY_LASTMONTH.'&playername='.$_GET['playername'].'&amp;ally_avg='.urlencode(ALLYHISTORY_ALLYAVG).'" border="0" alt="monthly history" /></td></tr>';
	if ($count > 1) {
		echo '
		<tr class="firstcolor"><td>&nbsp;</td></tr>
		<tr class="firstcolor"><td><img src="graph.php?typ=all&amp;playerid='.$_GET['details'].'&amp;text='.ALLYHISTORY_OVERALL.'&playername='.$_GET['playername'].'&amp;ally_avg='.urlencode(ALLYHISTORY_ALLYAVG).'" border="0" alt="overall history" /></td></tr>
		</table>
		';
	}


} else {
	// show allyhistory
	echo '
	<table cellpadding="4" cellspacing="0" border="0" width="90%" align="center">
	<tr class="tblhead"><td colspan="4">'.ALLYHISTORY_TBLHEAD.'</td></tr>
	<tr class="tblhead"><td>'.ALLYHISTORY_PLAYERNAME.'</td><td>'.ALLYHISTORY_SCORE.'</td><td>'.ALLYHISTORY_PROGESS.'</td><td>'.ALLYHISTORY_DETAILS.'</td></tr>
	';

	$query = "SELECT distinct(ogame_playerid) FROM $allyhistory ORDER BY playername";
	$res = mysql_query($query) or die(mysql_error());
	while ($line = mysql_fetch_object($res)) {
		$query2 = "SELECT * FROM $allyhistory WHERE ogame_playerid='".$line->ogame_playerid."' ORDER BY date_added DESC LIMIT 2";	 // get the last two entries
		$res2 = mysql_query($query2) or die(mysql_error());

		// get the difference between the last two entries (if there are two)
		$difference = 0;
		$temp = 0;
		$i = 1;
		while ($line2 = mysql_fetch_object($res2)) {
			$playername = $line2->playername;
			$playerid   = $line2->ogame_playerid;

			if ($i == 1) {
				$temp = $line2->score;
			} else {
				$difference = $temp - $line2->score;
			}
			$i++;
		}
		$score = $temp;

		// get css class for progress
		$diffclass = "ah_equal";
		if ($difference > 0) $diffclass = "ah_more";
		elseif ($difference < 0) $diffclass = "ah_less";

		echo '
		<tr class="firstcolor">
		<td align="center"><a class="link" href="show.php?spieler='.urlencode($playername).'&new=true">'.$playername.'</a></td>
		<td align="center">'.number_format($score,0,",",".").'</td>
		<td align="center" class="'.$diffclass.'">'.number_format($difference,0,",",".").'</td>
		<td align="center"><a href="allyhistory.php?details='.urlencode($playerid).'&amp;playername='.urlencode($playername).'" ><img src="../images/details.gif" border="0" /></a></td>
		</tr>
		';
	}


	echo '
	</table>
	';
}

makefooter();
?>